package com.liuyk.commonlib.adapter;

import android.content.Context;
import androidx.viewpager.widget.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.ArrayList;

/**
 * PagerAdapter基类
 * <p>
 *
 * @author liuyk
 */
public abstract class BasePagerAdapter<T> extends PagerAdapter {
    protected Context mContext;
    protected ArrayList<T> mItems;
    protected ArrayList<View> mViews;

    public BasePagerAdapter(Context context) {
        mContext = context;
        mItems = new ArrayList<>();
        mViews = new ArrayList<>();
    }

    public void setItems(ArrayList<T> items) {
        mItems = items;
    }

    public ArrayList<T> getItems() {
        return mItems;
    }

    public ArrayList<View> getViews() {
        return mViews;
    }

    public T getData(int position) {
        return mItems.get(position);
    }

    @Override
    public int getCount() {
        return mItems == null ? 0 : mItems.size();
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = LayoutInflater.from(mContext).inflate(resView(position), container, false);
        binderData(view, position);
        container.addView(view);
        mViews.add(view);
        return view;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View) object);
    }

    public abstract void binderData(View view, int position);

    public abstract int resView(int position);
}
